package com.ctsi.product.controller;


 import com.ctsi.commons.util.UtilDateTime;
 import com.ctsi.commons.util.UtilMisc;
 import com.ctsi.commons.util.UtilValidate;
 import com.ctsi.commons.util.request.UtilHttp;
 import com.ctsi.commons.util.response.Page;
 import com.ctsi.framework.common.response.UnifiedResponse;
 import com.ctsi.framework.mybatis.support.VoTemplate;
 import com.ctsi.framework.organise.security.UserInfo;
 import com.ctsi.framework.security.util.SecurityUtil;
 import com.ctsi.product.entity.*;
 import com.ctsi.product.service.*;
 import com.ctsi.product.vo.*;
 import com.ctsi.system.service.api.DictionayTreeServiceProxy;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.http.HttpStatus;
 import org.springframework.web.bind.annotation.*;

 import java.io.File;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;


 /**
 * ProductLibrary Controller 类
 * @author   liuhx
 * @description  ProductLibrary
 * @created	 Fri Nov 16 11:53:49 CST 2018
 */

@RestController
public class ProductLibraryController {
	private static final Logger logger=LoggerFactory.getLogger(ProductLibraryController.class);
	@Autowired
	private ProductLibraryService productLibraryService;
	@Autowired
	private DianzanNumberService dianzanNumberService;
	@Autowired
	private PinglunNumberService pinglunNumberService;
	@Autowired
	private LiulanControllerService liulanControllerService;
	@Autowired
    private UploadService uploadService;
	@Autowired
	private ProductTypeNameService productTypeNameService;
	@Value("${file-store.host}")
	private String fileServerHost;

	 /**
	  * 领导驾驶舱接口
	  */
	 @PostMapping(value = "/product/selectLeaderCockpit")
	 @ResponseStatus(HttpStatus.OK)
	 public UnifiedResponse<List<LeaderCockpitVO>> queryAnswerListById() {
		 List<LeaderCockpitVO> productLibraries = productLibraryService.selectLeaderCockpit();
		 return UnifiedResponse.ok(productLibraries);
	 }

	 /**
	  * @param id
	  * @return
	  */
	 @PostMapping(value = "/upload/removeFileById",produces = "application/json;charset=UTF-8")
	 @ResponseStatus(HttpStatus.OK)
	 public UnifiedResponse<Integer> removeFileById(@RequestParam("id") String id){
		 if (UtilValidate.isEmpty(id)){
			 return null;
		 }
		 int i = uploadService.deleteFile(id);
		 return UnifiedResponse.ok(i);
	 }
	/*
	* 查询点赞数量
	* selectDianZanNumByProductId
	* param:productId
	* */
	 @PostMapping(value = "/product/queryDianZanNum",produces = "application/json;charset=UTF-8")
	 @ResponseStatus(HttpStatus.OK)
	public UnifiedResponse<Long> selectDianZanNumByProductId(@RequestParam("productId") String productId){
	 	if (UtilValidate.isEmpty(productId)){
	 		return null;
		}
         Long o = (Long) dianzanNumberService.selectDianZanNumByProductId(productId);
         return UnifiedResponse.ok(o);
	 }
	 /**
	  * 查询收藏数量
	  */
	 @PostMapping(value = "/product/queryShouCangNum",produces = "application/json;charset=UTF-8")
	 @ResponseStatus(HttpStatus.OK)
	 public UnifiedResponse<Long> selectSCNumByProductId(@RequestParam("productId") String productId){
		 if (UtilValidate.isEmpty(productId)){
			 return null;
		 }
		 Long o = (Long) dianzanNumberService.selectSCNumByProductId(productId);
		 return UnifiedResponse.ok(o);
	 }

	 /**
	  *录入收藏记录
	  * @param productId
	  * @return
	  */
	 @PostMapping(value = "/product/addCollect",produces = "application/json;charset=UTF-8")
	 @ResponseStatus(HttpStatus.OK)
	 public UnifiedResponse<Integer> insertCollect(@RequestParam("productId") String productId){
		 UserInfo user = SecurityUtil.getUser();
		 DianzanNumber dianzanNumber=new DianzanNumber();
		 String userId = user.getUserId();
		 dianzanNumber.setUserid(userId);
		 dianzanNumber.setProduct_id(productId);
		dianzanNumber.setIs_collect(1);
		dianzanNumber.setCreatedate(System.currentTimeMillis());
		 int i = dianzanNumberService.insertCollect(dianzanNumber);
		 Long o = (Long) dianzanNumberService.selectSCNumByProductId(productId);
		 ProductLibrary productLibrary=new ProductLibrary();
		 productLibrary.setId(productId);
		 productLibrary.setShoucang_num(String.valueOf(o));
		 int i1 = productLibraryService.updateShouCangNumById(productLibrary);
		 return UnifiedResponse.ok(i1);
	 }


	 /*
	  * 查询评论数量
	  * selectPinLunNumByProductId
	  * param:productId
	  * */
	 @PostMapping(value = "/product/queryPinLunNum",produces = "application/json;charset=UTF-8")
	 @ResponseStatus(HttpStatus.OK)
	 public UnifiedResponse<Long> selectPinLunNumByProductId(@RequestParam("productId") String productId){
		 if (UtilValidate.isEmpty(productId)){
			 return null;
		 }
		 Long o = (Long) pinglunNumberService.selectPinLunNumByProductId(productId);
		 return UnifiedResponse.ok(o);
	 }

	 /*
	  * 查询浏览数量
	  * selectNumByProductId
	  * param:productId
	  * */
	 @PostMapping(value = "/product/queryLiuLanNum",produces = "application/json;charset=UTF-8")
	 @ResponseStatus(HttpStatus.OK)
	 public UnifiedResponse<Long> selectLiuLanNumByProductId(@RequestParam("productId") String productId){
		 if (UtilValidate.isEmpty(productId)){
			 return null;
		 }
		 Long o= (Long) liulanControllerService.selectNumByProductId(productId);
		 return UnifiedResponse.ok(o);
	 }

	 /*
	  * 每次点赞插入数据到t_dianzan表
	  * productId
	  * param:productId
	  * */
	 @PostMapping(value = "/product/addDianZan",produces = "application/json;charset=UTF-8")
	 @ResponseStatus(HttpStatus.OK)
	 public UnifiedResponse<Integer> insertDianzanNumber(@RequestParam("productId") String productId){
		 UserInfo user = SecurityUtil.getUser();
		 DianzanNumber dianzanNumber=new DianzanNumber();
		 String userId = user.getUserId();
         dianzanNumber.setUserid(userId);
		 dianzanNumber.setProduct_id(productId);
         int i2 = dianzanNumberService.selectIsDianZanCount(dianzanNumber);
         int i3 = dianzanNumberService.selectIsDianZanCount1(dianzanNumber);
         if(i2==0&&i3==0){
             dianzanNumber.setIs_dianzan(0);
             int i = dianzanNumberService.insertDianzanNumber(dianzanNumber);
         }else if(i2==1&&i3==0){
             dianzanNumber.setIs_dianzan(1);
             dianzanNumberService.updateIsDianZan(dianzanNumber);
         }else if(i2==0&&i3==1){
             dianzanNumber.setIs_dianzan(0);
             dianzanNumberService.updateIsDianZan(dianzanNumber);
         }
         //查询该产品点赞数量更新到产品表
         Long o = (Long) dianzanNumberService.selectDianZanNumByProductId(productId);
         ProductLibrary productLibrary=new ProductLibrary();
         productLibrary.setId(productId);
         productLibrary.setDianzan_num(String.valueOf(o));
         int i1 = productLibraryService.updateDianZanNumById(productLibrary);
         return UnifiedResponse.ok(i1);
	 }

	 /*
	  * 每次浏览插入数据到t_liulan表
	  * insertLiulanController
	  * param:productId
	  * */
	 @PostMapping(value = "/product/addLiuLan",produces = "application/json;charset=UTF-8")
	 @ResponseStatus(HttpStatus.OK)
	 public UnifiedResponse<Integer> insertLiulanController(@RequestParam("productId") String productId){
		 UserInfo user = SecurityUtil.getUser();
		 LiulanController liulanController=new LiulanController();
		 String userId = user.getUserId();
		 liulanController.setUserid(userId);
		 liulanController.setProductid(productId);
		 int i = liulanControllerService.insertLiulanController(liulanController);
		  //查询该产品浏览数量更新到产品表
         Long o = (Long) liulanControllerService.selectNumByProductId(productId);
         ProductLibrary productLibrary=new ProductLibrary();
         productLibrary.setId(productId);
         productLibrary.setLiulan_num(String.valueOf(o));
         int i1 = productLibraryService.updateLiuLanNumById(productLibrary);
         return UnifiedResponse.ok(i1);
	 }
	 /*
	  * 每次评论插入数据到t_pinglun表
	  * insertLiulanController
	  * param:productId
	  * */
	 @PostMapping(value = "/product/addPingLun",produces = "application/json;charset=UTF-8")
	 @ResponseStatus(HttpStatus.OK)
	 public UnifiedResponse<Integer> insertPinLunController(@RequestParam("productId") String productId,
															@RequestParam("remark") String remark){
		 UserInfo user = SecurityUtil.getUser();
		 String userId = user.getUserId();
		 PinglunNumber pinglunNumber=new PinglunNumber();
		 pinglunNumber.setUserid(userId);
		 pinglunNumber.setDepartment(user.getDepartName());
		 pinglunNumber.setCreate_date(System.currentTimeMillis());
		 pinglunNumber.setProduct_id(productId);
		 pinglunNumber.setRemark(remark);
         int i = pinglunNumberService.insertPinglunNumber(pinglunNumber);
		 //插入后获取该产品对应的评论数据量
         Long o = (Long) pinglunNumberService.selectPinLunNumByProductId(productId);
         ProductLibrary productLibrary=new ProductLibrary();
         productLibrary.setId(productId);
         productLibrary.setPinlun_num(String.valueOf(o));
         int i1 = productLibraryService.updatePinLunNumById(productLibrary);
         return UnifiedResponse.ok(i1);
	 }
	 /*
	  * 根据产品id查询评论列表
	  * queryPinLunByProductId
	  * param:productId
	  * */
	 @PostMapping(value = "/product/queryPinLunByProductId",consumes = "application/json",produces = "application/json;charset=UTF-8")
	 @ResponseStatus(HttpStatus.OK)
	 public UnifiedResponse<Page<PingLunLibVO>> queryPinLunByProductId(@RequestBody PingLunQueryPageParam ppp){
		 Page<PinglunNumber> result = pinglunNumberService.queryPinLunByProductId(UtilHttp.filterBlank(UtilMisc.toMap(ppp)),ppp.getStart(),ppp.getPageSize());
			List<PingLunLibVO> pingLunLibVOS=result.getResult().stream().map(lib ->{
				PingLunLibVO pingLunLibVO=voTemplate.defaultExcute(new PingLunLibVO(),lib);
				pingLunLibVO.setCreateDateStr(UtilDateTime.longToString(lib.getCreate_date()));
				return pingLunLibVO;
			}).collect(Collectors.toList());
		 return UnifiedResponse.ok(new Page<>(ppp.getStart(),result.getTotalCount(),result.getPageSize(),pingLunLibVOS));
	 }

	 /*
	  * 实时更新评论数量
	  * updatePinLunNumById
	  * param:productLibid
	  * */
	 @PostMapping(value = "/product/updatePinLunNumById",consumes = "application/json",produces = "application/json;charset=UTF-8")
	 @ResponseStatus(HttpStatus.OK)
	 public UnifiedResponse<Integer> updatePinLunNumById(@RequestParam("productId") String productId){
		 Long pinglunNum = (Long) pinglunNumberService.selectPinLunNumByProductId(productId);
		 ProductLibrary productLibrary =new ProductLibrary();
		 productLibrary.setId(productId);
		 productLibrary.setPinlun_num(String.valueOf(pinglunNum));
		 int i = productLibraryService.updatePinLunNumById(productLibrary);
		 return UnifiedResponse.ok(i);
	 }
	 /*
	  * 实时更新点赞数量
	  * updatePinLunNumById
	  * param:productId
	  * */
	 @PostMapping(value = "/product/updateDianZanNumById",consumes = "application/json",produces = "application/json;charset=UTF-8")
	 @ResponseStatus(HttpStatus.OK)
	 public UnifiedResponse<Integer> updateDianZanNumById(@RequestParam("productId") String productId){
		 Long num = (Long) dianzanNumberService.selectDianZanNumByProductId(productId);
         ProductLibrary productLibrary =new ProductLibrary();
		 productLibrary.setId(productId);
		 productLibrary.setDianzan_num(String.valueOf(num));
		 int i = productLibraryService.updateDianZanNumById(productLibrary);
		 return UnifiedResponse.ok(i);
	 }
	 /*
	  * 实时更新浏览数量
	  * updatePinLunNumById
	  * param:productId
	  * */
	 @PostMapping(value = "/product/updateLiuLanNumById",consumes = "application/json",produces = "application/json;charset=UTF-8")
	 @ResponseStatus(HttpStatus.OK)
	 public UnifiedResponse<Integer> updateLiuLanNumById(@RequestParam("productId") String productId){
		 Long liulanNum = (Long) liulanControllerService.selectNumByProductId(productId);
		 ProductLibrary productLibrary =new ProductLibrary();
		 productLibrary.setId(productId);
		 productLibrary.setLiulan_num(String.valueOf(liulanNum));
		 int i = productLibraryService.updateLiuLanNumById(productLibrary);
		 return UnifiedResponse.ok(i);
	 }


	 @Autowired
     private DictionayTreeServiceProxy dictionayTreeServiceProxy;
	 @Autowired
     private VoTemplate voTemplate;

	 /*
	  *添加或修改产品
	  * addorEditProduct
	  * param:productLibrary
	  * */
	 @PostMapping(value = "/product/addorEditProduct",consumes = "application/json",produces = "application/json;charset=UTF-8")
	 @ResponseStatus(HttpStatus.OK)
	 public UnifiedResponse<Integer> addorEditProduct(@RequestBody ProductLibrary productLibrary){
		 UserInfo user = SecurityUtil.getUser();
         if(UtilValidate.isEmpty(productLibrary.getId())){
             String userId = user.getUserId();
             productLibrary.setUserid(userId);
             productLibrary.setFullName(user.getFullName());
             productLibrary.setCreate_date(System.currentTimeMillis());
             productLibrary.setLiulan_num("0");
             productLibrary.setDianzan_num("0");
             productLibrary.setPinlun_num("0");
             productLibrary.setId_default(0);
             if(UtilValidate.isNotEmpty(productLibrary.getLogo_url())){
             	String LogoUrl=productLibrary.getLogo_url();
             	productLibrary.setLogo_url(LogoUrl);
			 }
             int i = productLibraryService.insertProductLibrary(productLibrary);
             if(UtilValidate.isNotEmpty(productLibrary.getProduct_type())){
				 String[] arr = productLibrary.getProduct_type().split(",");
				 for (String type : productLibrary.getProduct_type().split(",")) {
					 ProductTypeName productTypeName=new ProductTypeName();
					 productTypeName.setId(productLibrary.getId());
				 	productTypeName.setProducttype(type);
					 productTypeNameService.insertProductLibrary(productTypeName);
				 }
			 }
			 if(UtilValidate.isNotEmpty(productLibrary.getZhuanti())){
				 String[] arr = productLibrary.getZhuanti().split(",");
				 for (String type : arr) {
					 ProductTypeName productTypeName=new ProductTypeName();
					 productTypeName.setId(productLibrary.getId());
					 productTypeName.setZhuanti_type(type);
					 productTypeNameService.insertProductLibrary(productTypeName);
				 }
			 }
			 List<FileMetaVO> resId = productLibrary.getResourceIds();
             if (UtilValidate.isNotEmpty(resId)) {
				 resId.stream().forEach(id -> {
					 Upload upload=new Upload();
					 upload.setId(id.getId());
					 upload.setUserid(user.getUserId());
					 upload.setIsdefault("0");
					 upload.setFilename(id.getFileName());
					 upload.setFilesize(String.valueOf(id.getFileSize()));
					 upload.setSavepath(id.getSavePath());
					 upload.setCreatetime(System.currentTimeMillis());
					 upload.setQuestion_id(productLibrary.getId());
					 uploadService.insertUpload(upload);
				 });
			 }
             return UnifiedResponse.ok(i);
         }else {
			 String imgurl="";
			 String name="";
			 String name1="";
			 if(UtilValidate.isNotEmpty(productLibrary.getLogo_url())){
				 String url=productLibrary.getLogo_url();
				 String temp[] = url.replaceAll("\\\\","/").split("/");
				 if (temp.length > 1) {
					 name = temp[temp.length - 1];
					 name1= temp[temp.length - 2];
				 }
				 if(url.startsWith("http")==true && name1.equals("images")==false){
					 imgurl= name1+File.separator+name;
				 }else if(name1.equals("images")==true) {
					 imgurl=null;
				 }else {
				 	imgurl=url;
				 }
				 productLibrary.setLogo_url(imgurl);
			 }
			 List<FileMetaVO> resId = productLibrary.getResourceIds();
			 if (UtilValidate.isNotEmpty(resId)) {
				 resId.stream().forEach(id -> {
					 Upload upload=new Upload();
					 upload.setId(id.getId());
					 upload.setUserid(user.getUserId());
					 upload.setIsdefault("0");
					 upload.setFilename(id.getFileName());
					 upload.setFilesize(String.valueOf(id.getFileSize()));
					 upload.setSavepath(id.getSavePath());
					 upload.setCreatetime(System.currentTimeMillis());
					 upload.setQuestion_id(productLibrary.getId());
					 uploadService.insertUpload(upload);
				 });
			 }
             productLibrary.setUpdate_date(System.currentTimeMillis());
             productTypeNameService.removeProductTypeName(productLibrary.getId());
			 if(UtilValidate.isNotEmpty(productLibrary.getProduct_type())){
				 String[] arr = productLibrary.getProduct_type().split(",");
				 for (String type : productLibrary.getProduct_type().split(",")) {
					 ProductTypeName productTypeName=new ProductTypeName();
					 productTypeName.setId(productLibrary.getId());
					 productTypeName.setProducttype(type);
					 productTypeNameService.insertProductLibrary(productTypeName);
				 }
			 }
			 if(UtilValidate.isNotEmpty(productLibrary.getZhuanti())){
				 String[] arr = productLibrary.getZhuanti().split(",");
				 for (String type : arr) {
					 ProductTypeName productTypeName=new ProductTypeName();
					 productTypeName.setId(productLibrary.getId());
					 productTypeName.setZhuanti_type(type);
					 productTypeNameService.insertProductLibrary(productTypeName);
				 }
			 }
             int i = productLibraryService.updateByPrimaryKeySelective(productLibrary);
             return UnifiedResponse.ok(i);
         }
	 }

	 /*
	  *删除产品（updatmse isDefault=1,默认为0）
	  * deleteByPrimaryKey
	  * param:productId
	  * */
	 @PostMapping(value = "/product/deleteByPrimaryKey")
	 public UnifiedResponse<String> deleteByPrimaryKey(@RequestParam(value = "ids") String ids){
         if (UtilValidate.isNotEmpty(ids)) {
             String[] arr = ids.split(",");
             int result = Stream.of(arr).filter(id -> UtilValidate.isNotEmpty(id)).mapToInt(id -> {
                 try {
                     ProductLibrary productLibrary=new ProductLibrary();
                     productLibrary.setId(id);
                     productLibrary.setId_default(1);
                     uploadService.removeUpload(id);//删除upload表数据
                     return this.productLibraryService.deleteByPrimaryKey(productLibrary);
                 }catch (Exception e){
                     logger.error("{}",e);
                     return 0;
                 }
             }).sum();
             if(arr.length >1){
                 return UnifiedResponse.ok("选中"+arr.length+"条数据，成功删除"+result+"条数据！");
             }else {
                 return UnifiedResponse.ok("删除成功!");
             }
         }
		 return UnifiedResponse.ok("删除成功!");
	 }



	 /*
	  *根据productId获取该产品信息
	  * selectProductById
	  *param:productId
	  * */
	 @PostMapping(value = "/product/selectProductById",produces = "application/json;charset=UTF-8")
	 @ResponseStatus(HttpStatus.OK)
	 public UnifiedResponse<ProductLibraryEditVO> selectProductById (@RequestParam(value = "productId") String productId){
		 if (UtilValidate.isEmpty(productId)){
			 return UnifiedResponse.ok(null);
		 }
		 ProductLibrary productLibrary = productLibraryService.selectProductById(productId);
		 UserInfo user = SecurityUtil.getUser();
		 DianzanNumber dianzanNumber=new DianzanNumber();
		 String userid = user.getUserId();
		 dianzanNumber.setUserid(userid);
		 dianzanNumber.setProduct_id(productId);
		 int i = dianzanNumberService.selectIsDianZanCount(dianzanNumber);
		 int i1 = dianzanNumberService.selectIsCollect(dianzanNumber);
//		 级联回显已注释
//		 List<IDictionaryTreeData> datas = dictionayTreeServiceProxy.getTreeDataChain("support-type",productLibrary.getProduct_type());
//		 List<String> codes = datas.stream().map(data -> data.getCode()).collect(Collectors.toList());
		 ProductLibraryEditVO vo = new ProductLibraryEditVO();
		 vo.setIs_show(i);
		 vo.setCollect(i1);
		 vo = voTemplate.defaultExcute(vo,productLibrary);
//		 vo.setCodes(codes);
		 Map<String,Object> param = new HashMap<>();
		 param.put("questionId",productLibrary.getId());
		 List<Upload> uploads = uploadService.find(param);
		 List<FileResourceVO> fvos =uploads.stream().map(upload -> {
			 FileResourceVO fvo = new FileResourceVO();
			 String fileName = upload.getFilename();
			 String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
			 //fvo.setDuration();
			 fvo.setFileId(upload.getId());
			 fvo.setFileName(fileName);
			 fvo.setFileSize(upload.getFilesize());
			 fvo.setFileType(suffix.equalsIgnoreCase("mp4") ? "mp4" : "pdf");
			 String webPath = upload.getSavepath().replace("\\","/");
			 String url = (fileServerHost.endsWith("/") ? fileServerHost : fileServerHost.concat("/"))+
					 (webPath.startsWith("/") ? "files/org/api/file" :"files/org/api/file/")+webPath;
			 fvo.setUrl(url);
			 return fvo;
		 }).collect(Collectors.toList());
		 vo.setResourceUrls(fvos);
		 Optional<FileResourceVO> optionalFileResourceVO = fvos.stream().findFirst();
		 if (optionalFileResourceVO.isPresent()) {
			 vo.setMp4_url(optionalFileResourceVO.get().getUrl());
		 }
		 vo.setCreateDateStr(UtilDateTime.longToString(productLibrary.getCreate_date()));
		 if (UtilValidate.isEmpty(productLibrary.getLogo_url())) {
			 String logoUrl = null;
			 if (productLibrary.getFile_type().equalsIgnoreCase("mp4")){
				 logoUrl = (fileServerHost.endsWith("/") ? fileServerHost : fileServerHost.concat("/"))+"files/org/images/mp4.png?ver="+System.currentTimeMillis();
			 }else {
				 logoUrl = (fileServerHost.endsWith("/") ? fileServerHost : fileServerHost.concat("/"))+"files/org/images/pdf.png?ver="+System.currentTimeMillis();
			 }
			 vo.setLogo_url(logoUrl);
		 }else if(UtilValidate.isNotEmpty(productLibrary.getLogo_url())){
			 String webPath = productLibrary.getLogo_url().replace("\\", "/");
			 String url = (fileServerHost.endsWith("/") ? fileServerHost : fileServerHost.concat("/")) +
					 (webPath.startsWith("/") ? "files/org/api/file" : "files/org/api/file/") + webPath;
			 vo.setLogo_url(url);
		 }
		 return UnifiedResponse.ok(vo);
	 }

	 /*
	  * 多条件查询产品列表
	  * queryListProductLib
	  * param:ProductQueryPageParam
	  * */
	 @PostMapping(value = "/product/queryListProductLib",consumes = "application/json",produces = "application/json;charset=UTF-8")
	 @ResponseStatus(HttpStatus.OK)
	 public UnifiedResponse<Page<ProductLibVO>> queryListProductLib(@RequestBody ProductQueryPageParam ppp){
	 	if (UtilValidate.isEmpty(ppp.getZuixin())){
	 		ppp.setZuixin("0");
		}
		 if(UtilValidate.isNotEmpty(ppp.getStartDate())){
			 ppp.setStartDate(String.valueOf(UtilDateTime.toLong(ppp.getStartDate())));
		 }
		 if (UtilValidate.isNotEmpty(ppp.getEndDate())){
			 ppp.setEndDate(String.valueOf(UtilDateTime.toLong(ppp.getEndDate())));
		 }
	     //处理分类参数
		 Map<String,Object> params = UtilHttp.filterBlank(UtilMisc.toMap(ppp));
	     params.remove("product_type");
	     params.remove("zhuanti");
	     if (UtilValidate.isNotEmpty(ppp.getProduct_type())) {
	     	String[] productTypes = ppp.getProduct_type().split(",");
			params.put("productTypes",productTypes);
		 }
		 if (UtilValidate.isNotEmpty(ppp.getHangye())) {
			 String[] hangyeTypes = ppp.getHangye().split(",");
			 params.put("hangyeTypes",hangyeTypes);
		 }
		 if(UtilValidate.isNotEmpty(ppp.getZhuanti())){
		 	String[] zhuantiTypes = ppp.getZhuanti().split(",");
		 	params.put("zhuantiTypes",zhuantiTypes);
		 }
         Page<ProductLibrary> result = productLibraryService.queryListProductLib(params,ppp.getStart(),ppp.getPageSize());
		 for (ProductLibrary productLibrary:result.getResult()){
			 Long o = (Long) dianzanNumberService.selectSCNumByProductId(productLibrary.getId());
			 ProductLibrary productLibrary1=new ProductLibrary();
			 productLibrary1.setId(productLibrary.getId());
			 productLibrary1.setShoucang_num(String.valueOf(o));
			 int i1 = productLibraryService.updateShouCangNumById(productLibrary1);
		 }
	     List<ProductLibVO> vos = result.getResult().stream().map(lib -> {
		     ProductLibVO vo = voTemplate.defaultExcute(new ProductLibVO(),lib);
			 if (UtilValidate.isEmpty(lib.getLogo_url())) {
				 String logoUrl = null;
				 if (lib.getFile_type().equalsIgnoreCase("mp4")){
					 logoUrl = (fileServerHost.endsWith("/") ? fileServerHost : fileServerHost.concat("/"))+"files/org/images/mp4.png?ver="+System.currentTimeMillis();
				 }else {
					 logoUrl = (fileServerHost.endsWith("/") ? fileServerHost : fileServerHost.concat("/"))+"files/org/images/pdf.png?ver="+System.currentTimeMillis();
				 }
				 vo.setLogo_url(logoUrl);
			 }else if(UtilValidate.isNotEmpty(lib.getLogo_url())) {
				 String webPath = lib.getLogo_url().replace("\\", "/");
				 String url = (fileServerHost.endsWith("/") ? fileServerHost : fileServerHost.concat("/")) +
						 (webPath.startsWith("/") ? "files/org/api/file" : "files/org/api/file/") + webPath;
				 vo.setLogo_url(url);
			 }
		     Map<String,Object> param = new HashMap<>();
		     param.put("questionId",lib.getId());
			 List<Upload> uploads = uploadService.find(param);
			 List<FileResourceVO> fvos =uploads.stream().map(upload -> {
				 FileResourceVO fvo = new FileResourceVO();
				 String fileName = upload.getFilename();
				 String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
				 //fvo.setDuration();
				 fvo.setFileName(fileName);
				 fvo.setFileSize(upload.getFilesize());
				 fvo.setFileType(suffix.equalsIgnoreCase("mp4") ? "mp4" : "pdf");
				 String webPath = upload.getSavepath().replace("\\","/");
				 String url = (fileServerHost.endsWith("/") ? fileServerHost : fileServerHost.concat("/"))+
						 (webPath.startsWith("/") ? "files/org/api/file" :"files/org/api/file/")+webPath;
				 fvo.setUrl(url);
				 return fvo;
			 }).collect(Collectors.toList());
			 vo.setResourceUrls(fvos);
			 Optional<FileResourceVO> optionalFileResourceVO = fvos.stream().findFirst();
			 if (optionalFileResourceVO.isPresent()) {
				 vo.setMp4_url(optionalFileResourceVO.get().getUrl());
			 }
		     vo.setCreateDateStr(UtilDateTime.longToString(lib.getCreate_date()));
		     return vo;
         }).collect(Collectors.toList());
	     //long start, long totalSize, int pageSize, List<E> data
	     return UnifiedResponse.ok(new Page<>(ppp.getStart(),result.getTotalCount(),result.getPageSize(),vos));
	 }
	 /*
	 * 当前登陆用户  评论过的产品列表
	 * */
	 @PostMapping(value = "/product/queryProLibLoginById",consumes = "application/json",produces = "application/json;charset=UTF-8")
	 @ResponseStatus(HttpStatus.OK)
	 public UnifiedResponse<Page<ProductLibVO>> queryProLibLoginById(@RequestBody ProductQueryPageParam ppp){
	 	if(UtilValidate.isEmpty(ppp.getUserid())){
			ppp.setUserid(SecurityUtil.getUser().getUserId());
		}
		 Page<ProductLibrary> result = productLibraryService.selectProLibByLoginId(UtilHttp.filterBlank(UtilMisc.toMap(ppp)),ppp.getStart(),ppp.getPageSize());
		 List<ProductLibVO> vos = result.getResult().stream().map(lib -> {
			 ProductLibVO vo = voTemplate.defaultExcute(new ProductLibVO(),lib);
			 if(UtilValidate.isEmpty(lib.getLogo_url())){
				 String logoUrl = null;
				 if (lib.getFile_type().equalsIgnoreCase("mp4")){
					 logoUrl = (fileServerHost.endsWith("/") ? fileServerHost : fileServerHost.concat("/"))+"files/org/images/mp4.png?ver="+System.currentTimeMillis();
				 }else {
					 logoUrl = (fileServerHost.endsWith("/") ? fileServerHost : fileServerHost.concat("/"))+"files/org/images/pdf.png?ver="+System.currentTimeMillis();
				 }
				 vo.setLogo_url(logoUrl);
			 }else if(UtilValidate.isNotEmpty(lib.getLogo_url())){
				 String webPath = lib.getLogo_url().replace("\\", "/");
				 String url = (fileServerHost.endsWith("/") ? fileServerHost : fileServerHost.concat("/")) +
						 (webPath.startsWith("/") ? "files/org/api/file" : "files/org/api/file/") + webPath;
				 vo.setLogo_url(url);
			 }
			 vo.setCreateDateStr(UtilDateTime.longToString(lib.getCreate_date()));
			 return vo;
		 }).collect(Collectors.toList());
		 return UnifiedResponse.ok(new Page<>(ppp.getStart(),result.getTotalCount(),result.getPageSize(),vos));
	 }
	 /**
	  * 收藏列表
	  */
	 @PostMapping(value = "/product/queryProCollectList",consumes = "application/json",produces = "application/json;charset=UTF-8")
	 @ResponseStatus(HttpStatus.OK)
	 public UnifiedResponse<Page<ProductLibVO>> queryProCollectList(@RequestBody ProductQueryPageParam ppp){
	 	if(UtilValidate.isEmpty(ppp.getUserid())){
			ppp.setUserid(SecurityUtil.getUser().getUserId());
		}
		 Page<ProductLibrary> result = productLibraryService.selectProCollectByUserId(UtilHttp.filterBlank(UtilMisc.toMap(ppp)),ppp.getStart(),ppp.getPageSize());
		 List<ProductLibVO> vos = result.getResult().stream().map(lib -> {
			 ProductLibVO vo = voTemplate.defaultExcute(new ProductLibVO(),lib);
			 if (UtilValidate.isEmpty(lib.getLogo_url())) {
				 String logoUrl = null;
				 if (lib.getFile_type().equalsIgnoreCase("mp4")){
					 logoUrl = (fileServerHost.endsWith("/") ? fileServerHost : fileServerHost.concat("/"))+"files/org/images/mp4.png?ver="+System.currentTimeMillis();
				 }else {
					 logoUrl = (fileServerHost.endsWith("/") ? fileServerHost : fileServerHost.concat("/"))+"files/org/images/pdf.png?ver="+System.currentTimeMillis();
				 }
				 vo.setLogo_url(logoUrl);
			 }else if(UtilValidate.isNotEmpty(lib.getLogo_url())) {
				 String webPath = lib.getLogo_url().replace("\\", "/");
				 String url = (fileServerHost.endsWith("/") ? fileServerHost : fileServerHost.concat("/")) +
						 (webPath.startsWith("/") ? "files/org/api/file" : "files/org/api/file/") + webPath;
				 vo.setLogo_url(url);
			 }
			 vo.setCreateDateStr(UtilDateTime.longToString(lib.getCreate_date()));
			 return vo;
		 }).collect(Collectors.toList());
		 return UnifiedResponse.ok(new Page<>(ppp.getStart(),result.getTotalCount(),result.getPageSize(),vos));
	 }
	 /**
	  * 学习记录
	  */
	 @PostMapping(value = "/product/queryProStudyList",consumes = "application/json",produces = "application/json;charset=UTF-8")
	 @ResponseStatus(HttpStatus.OK)
	 public UnifiedResponse<Page<ProductLibVO>> queryProStudyList(@RequestBody ProductQueryPageParam ppp){
	 		ppp.setUserid(SecurityUtil.getUser().getUserId());
		 Page<ProductLibrary> result = productLibraryService.selectProStudyByUserId(UtilHttp.filterBlank(UtilMisc.toMap(ppp)),ppp.getStart(),ppp.getPageSize());
		 List<ProductLibVO> vos = result.getResult().stream().map(lib -> {
			 ProductLibVO vo = voTemplate.defaultExcute(new ProductLibVO(),lib);
			 if (UtilValidate.isEmpty(lib.getLogo_url())) {
				 String logoUrl = null;
				 if (lib.getFile_type().equalsIgnoreCase("mp4")){
					 logoUrl = (fileServerHost.endsWith("/") ? fileServerHost : fileServerHost.concat("/"))+"files/org/images/mp4.png?ver="+System.currentTimeMillis();
				 }else {
					 logoUrl = (fileServerHost.endsWith("/") ? fileServerHost : fileServerHost.concat("/"))+"files/org/images/pdf.png?ver="+System.currentTimeMillis();
				 }
				 vo.setLogo_url(logoUrl);
			 }else if(UtilValidate.isNotEmpty(lib.getLogo_url())) {
				 String webPath = lib.getLogo_url().replace("\\", "/");
				 String url = (fileServerHost.endsWith("/") ? fileServerHost : fileServerHost.concat("/")) +
						 (webPath.startsWith("/") ? "files/org/api/file" : "files/org/api/file/") + webPath;
				 vo.setLogo_url(url);
			 }
			 vo.setCreateDateStr(UtilDateTime.longToString(lib.getCreate_date()));
			 return vo;
		 }).collect(Collectors.toList());
		 return UnifiedResponse.ok(new Page<>(ppp.getStart(),result.getTotalCount(),result.getPageSize(),vos));
	 }
 }
